package com.fingard.dsp.bank.directbank.payease01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;

/** F.cruise
 *  首信易单笔代付
 */
public class PayEase01SinglePay extends PayEase01Base implements ISinglePay {
	/**
	 * 提现请求提交
	 */
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail = d_190802(p_req1908);
		
		if(tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)){
			tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
		}
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}

	//190802对外支付
	private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception{
		RetAtsSglDetail retDetail = new RetAtsSglDetail();
		
		ActSetItem actItem = getBankConfig().getActSetItem(p_req1908.singleDetail.payAct, "");
		if (actItem == null) {
			retDetail.respCode = "0";
			retDetail.respInfo = "未找到账号" + p_req1908.singleDetail.payAct + "对应的商户配置信息";
			retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
			retDetail.transAmt = p_req1908.singleDetail.payAmount;
			retDetail.transState = PaymentState.FAIL_Payment;
			retDetail.payInfoCode = FailCode.E9981;
			
			return retDetail;
		}
		
		
		StringBuilder req_Json = new StringBuilder();
		// 拼接提现交易请求JSON
		req_Json.append("{");
		req_Json.append("\"serlNum\":\"" + p_req1908.singleDetail.askNumber + "\","); // 交易流水号;系统交易流水唯一标识
		req_Json.append("\"groupId\":\"" + actItem.merchantId + "\",");// P2P商户编号
		req_Json.append("\"transCode\":\"TRS001006\","); // 交易码
		req_Json.append("\"reqTime\":\"" + Format.DateTimeFormat.format(new Date()) + "\",");// 请求日期
		req_Json.append("\"operationCode\":\"" + getBankConfig().getMapValueByKey("operationCode", "0") + "\",");// 业务操作码
		req_Json.append("\"authId\":\"\",");// 授权号
		req_Json.append("\"user\":\"" + actItem.merchantId + "\",");// P2P网站用户注册名
		req_Json.append("\"mobile\":\"\",");// 注册用户手机号
		req_Json.append("\"idType\":\"\",");// 开户证件类型
		req_Json.append("\"id\":\"\","); // 开户证件号
		req_Json.append("\"accName\":\"" + p_req1908.singleDetail.recName + "\",");// 开户名
		req_Json.append("\"accProvince\":\"\",");// 开户省
		req_Json.append("\"accCity\":\"\",");// 开户市
		req_Json.append("\"accBank\":\"" + p_req1908.singleDetail.recBankName + "\",");// 开户银行名称
		req_Json.append("\"accBranchName\":\"\",");// 开户分行支行名称
		req_Json.append("\"accNum\":\"" + p_req1908.singleDetail.recAct + "\",");// 银行账号
		
		//账号类型     00 -- 卡；  01 -- 存折	     默认00
		if (p_req1908.singleDetail.cardType.equals("1")) {
			req_Json.append("\"accType\":\"" + "01" + "\",");
		} else {
			req_Json.append("\"accType\":\"" + "00" + "\",");
		}
		
		// 账号类别  PR-对私	PU-对公	默认PR
		if (p_req1908.singleDetail.isPrivate.equals("1")) {
			req_Json.append("\"accProp\":\"" + "PR" + "\",");
		} else {
			req_Json.append("\"accProp\":\"" + "PU" + "\",");
		}
		
		req_Json.append("\"amount\":\"" + p_req1908.singleDetail.payAmount + "\",");// 提现金额
		req_Json.append("\"branchId\":\"0\",");// P2P子公司编号
		req_Json.append("\"merdata1\":\"\",");// 预留字段1
		req_Json.append("\"merdata2\":\"\",");// 预留字段2
		req_Json.append("\"channel\":\"0\",");// 代付渠道号  0为提现;   1为验卡
		req_Json.append("\"accBranchNum\":\"" + p_req1908.singleDetail.cnapsCode + "\",");// 联行号
		req_Json.append("\"totalAmount\":\"" + p_req1908.singleDetail.payAmount + "\",");// 扣取虚拟账户金额
		req_Json.append("\"fee\":\"0.00\",");// 手续费
		
		// 是否是实时通道  是-U1；	否-U2；	默认值：U1
		if (p_req1908.reqHeader.commandCode.equals("190801")) {
			req_Json.append("\"urgency\":\"U1\",");
		} else {
			req_Json.append("\"urgency\":\"U2\",");
		}
		
		req_Json.append("\"amountSplit\":\"1\",");// 拆分标识
		req_Json.append("\"pin\":\"\",");// 用户密码
		req_Json.append("\"channelCode\":\"\",");// 渠道号
		req_Json.append("\"url\":\"\"");// 渠道连接地址
		req_Json.append("}");
		
		// 加密前JSON
		WriteBankLogLn("加密前JSON:" + req_Json.toString());
		
		// 发送首信易
		String[] tmpStrRet = sendToBank("payment", req_Json.toString(), actItem);
		
		// 解析首信易返回
		if(tmpStrRet[0].length() == 0) {
			WriteBankLogLn2("解密前JSON:" + tmpStrRet[1]);
			
			// 解密JSON (DES解密)
			String tmpRespJson = EncDecUtil.dec(actItem.saltKey, tmpStrRet[1]);
			
			// 首信易返回JSON (解密后JSON)
			WriteBankLogStep3(tmpRespJson);
			
			if (tmpRespJson.startsWith("{")) {
				String[] tmpMsgs =tmpRespJson.split(",");
				
				String returnCode = "";
				String returnMsg = "";
				String transAmt = "";
				
				/*
				 * 循环遍历JSON，获取JSON串中的有效信息
				 */
				for(int i=0;i<tmpMsgs.length;i++) {
					String[] values = tmpMsgs[i].split(":");
					if(values.length>1){
						if(values[0].indexOf("returnCode")>=0){
							returnCode = values[1].replace("\"", "").replace("{", "").replace("}", "").replace("[", "").replace("]", ""); 
						}else if(values[0].indexOf("returnMsg")>=0){
							returnMsg = values[1].replace("\"", "").replace("{", "").replace("}", "").replace("[", "").replace("]", ""); 
						}else if(values[0].indexOf("amount")>=0){
							String amount = values[1].replace("\"", "").replace("{", "").replace("}", "").replace("[", "").replace("]", ""); 
							transAmt = Format.DecimalAmtFormat.format(Double.parseDouble(amount));
						}
					}
				}
				
				retDetail.respCode = "0";
				retDetail.respInfo = returnMsg;
				retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
				retDetail.transAmt = transAmt;
				retDetail.transState = returnCode;
				retDetail.payInfoCode = retDetail.transState;
			} else {
				retDetail.respCode = "-1";
				retDetail.respInfo = tmpRespJson;
			}
		}else if(tmpStrRet[0].equals(ConnState.FAIL)){ // 通讯失败
			retDetail.respCode = "0";
			retDetail.respInfo = tmpStrRet[1];
			retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
			retDetail.transAmt = p_req1908.singleDetail.payAmount;
			retDetail.transState = PaymentState.FAIL_Payment;
			retDetail.payInfoCode = tmpStrRet[0];
		}else{
			retDetail.respCode = "-1";
			retDetail.respInfo = tmpStrRet[1];
		}
		
		return retDetail;
	}

	/**
	 * 单笔代付状态查询
	 */
	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
		/**
		 * 首信易有异步通知接口，该接口无效
		 */
		return null;
	}

	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		return null;
	}
}
